import path from "node:path"
import type { IMiddleware } from "boot/interface"
import { bodyParser } from 'boot/web/middleware'

//
const cwd = process.cwd()
const uploadDir = path.join(cwd, 'public/upload/')
console.log(`uploadDir = ${uploadDir}`)

export class BodyParserMiddleware implements IMiddleware {

  use = bodyParser({
    multipart: true, // 支持文件上传ze
    encoding: 'gzip',
    formidable: {
      uploadDir: uploadDir, // 设置文件上传目录
      keepExtensions: true,    // 保持文件的后缀
      multipart: true,    // 是否支持多文件上传
      maxFields: 32, // 文件的数量
      maxFieldsSize: 2 * 1024 * 1024, // 文件上传大小
      onFileBegin: (name, file) => { // 文件上传前的一些设置操作
        // console.log(`name: ${name}`);
        // console.log(file);
      },
    }
  })
}
